package org.tbjd.client.guis.projectSwitcher;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.ProjectsGridConstants;
import org.tbjd.client.dndControllers.projectSwitcher.GridRowPickupDragController;
import org.tbjd.client.dndControllers.projectSwitcher.StatusDropController;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.helpers.ProjectStatusFilterHelper;
import org.tbjd.client.images.projectSwitcher.status.StatusImageBundle;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.Displayer;
import org.tbjd.client.interfaces.HasDragController;
import org.tbjd.client.interfaces.Observer;
import org.tbjd.client.listeners.projectSwitcher.StatusFiltersListener;
import org.tbjd.client.mediators.AbstractMediator;

import com.allen_sauer.gwt.dnd.client.DragController;
import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class StatusGui extends Composite implements Displayer, Observer, Decisionmaker, Builder, HasDragController {

	private AbstractMediator mediator = null;
	private static StatusGui instance = null;
	private VerticalPanel container = new VerticalPanel();
	private GridRowPickupDragController dragController = null;
	private List<StatusDropController> dropControllers = new ArrayList<StatusDropController>();
	private ToggleButton toggleSearch = null;

	private StatusGui() {
		build();
		initWidget(container);
	}

	public static StatusGui getInstance() {
		if (instance == null) {
			instance = new StatusGui();
		}
		return instance;
	}

	public void build() {
		StatusFiltersListener statusFiltersListener = new StatusFiltersListener();
		StatusImageBundle statusImageBundle = (StatusImageBundle) GWT.create(StatusImageBundle.class);

		ToggleButton toggleRunning = new ToggleButton(statusImageBundle.run_bw_24x24().createImage(), statusImageBundle.run_color_24x24()
				.createImage());
		toggleRunning.addClickListener(statusFiltersListener);
		toggleRunning.setStyleName("statusWidget_button");
		toggleRunning.setTitle(String.valueOf(ProjectsGridConstants.RUNNING));
		AbsolutePanel runningDropTarget = new AbsolutePanel();
		runningDropTarget.add(toggleRunning);
		runningDropTarget.setTitle("runningDropTarget");
		StatusDropController runningDropController = new StatusDropController(runningDropTarget);
		dropControllers.add(runningDropController);
		container.add(runningDropTarget);

		ToggleButton toggleStopped = new ToggleButton(statusImageBundle.stop_bw_24x24().createImage(), statusImageBundle.stop_color_24x24()
				.createImage());
		toggleStopped.addClickListener(statusFiltersListener);
		toggleStopped.setStyleName("statusWidget_button");
		toggleStopped.setTitle(String.valueOf(ProjectsGridConstants.STOPPED));
		AbsolutePanel stoppedDropTarget = new AbsolutePanel();
		stoppedDropTarget.add(toggleStopped);
		stoppedDropTarget.setTitle("stoppedDropTarget");
		StatusDropController stoppedDropController = new StatusDropController(stoppedDropTarget);
		dropControllers.add(stoppedDropController);
		container.add(stoppedDropTarget);

		ToggleButton togglePaused = new ToggleButton(statusImageBundle.pause_bw_24x24().createImage(), statusImageBundle.pause_color_24x24()
				.createImage());
		togglePaused.addClickListener(statusFiltersListener);
		togglePaused.setStyleName("statusWidget_button");
		togglePaused.setTitle(String.valueOf(ProjectsGridConstants.PAUSED));
		AbsolutePanel pausedDropTarget = new AbsolutePanel();
		pausedDropTarget.add(togglePaused);
		pausedDropTarget.setTitle("pausedDropTarget");
		StatusDropController pausedDropController = new StatusDropController(pausedDropTarget);
		dropControllers.add(pausedDropController);
		container.add(pausedDropTarget);

		ToggleButton toggleFinished = new ToggleButton(statusImageBundle.finish_bw_24x24().createImage(), statusImageBundle.finish_color_24x24()
				.createImage());
		toggleFinished.addClickListener(statusFiltersListener);
		toggleFinished.setStyleName("statusWidget_button");
		toggleFinished.setTitle(String.valueOf(ProjectsGridConstants.FINISHED));
		AbsolutePanel finishedDropTarget = new AbsolutePanel();
		finishedDropTarget.add(toggleFinished);
		finishedDropTarget.setTitle("finishedDropTarget");
		StatusDropController finishedDropController = new StatusDropController(finishedDropTarget);
		dropControllers.add(finishedDropController);
		container.add(finishedDropTarget);

		ToggleButton toggleFavorites = new ToggleButton(statusImageBundle.star_bw_24x24().createImage(), statusImageBundle.star_color_24x24()
				.createImage());
		toggleFavorites.addClickListener(statusFiltersListener);
		toggleFavorites.setStyleName("statusWidget_button");
		toggleFavorites.setTitle(String.valueOf(ProjectsGridConstants.FAVORITES));
		AbsolutePanel favoritesDropTarget = new AbsolutePanel();
		favoritesDropTarget.add(toggleFavorites);
		favoritesDropTarget.setTitle("favoritesDropTarget");
		StatusDropController favoritesDropController = new StatusDropController(favoritesDropTarget);
		dropControllers.add(favoritesDropController);
		container.add(favoritesDropTarget);

		ToggleButton togglePriority = new ToggleButton(statusImageBundle.warn_bw_24x24().createImage(), statusImageBundle.warn_color_24x24()
				.createImage());
		togglePriority.addClickListener(statusFiltersListener);
		togglePriority.setStyleName("statusWidget_button");
		togglePriority.setTitle(String.valueOf(ProjectsGridConstants.PRIORITY));
		AbsolutePanel priorityDropTarget = new AbsolutePanel();
		priorityDropTarget.add(togglePriority);
		priorityDropTarget.setTitle("priorityDropTarget");
		StatusDropController priorityDropController = new StatusDropController(priorityDropTarget);
		dropControllers.add(priorityDropController);
		container.add(priorityDropTarget);

		toggleSearch = new ToggleButton(statusImageBundle.search_bw_24x24().createImage(), statusImageBundle.search_color_24x24().createImage());
		toggleSearch.setStyleName("statusWidget_button");
		toggleSearch.setTitle(String.valueOf(ProjectsGridConstants.SEARCH));
		toggleSearch.addClickListener(statusFiltersListener);
		container.add(toggleSearch);

	}

	public void decide(Object action) {
		System.out.println("StatusGui.decide(" + action + ")");
		delegate(action);
	}

	public void delegate(Object action) {
		System.out.println("StatusGui.delegate(" + action + ")");

		if (((Map<Integer, Object>) action).containsKey(NotificationsConstants.FETCH_PROJECTS)) {
			try {
				this.mediator.decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		}
	}

	public void display() {
	}

	public void handle(Object action) {
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.mediator.addObserver(this);
		for (StatusDropController dropController : this.dropControllers) {
			dropController.setMediator(this.mediator);
		}
	}

	public void setDragController(DragController dragController) {
		this.dragController = (GridRowPickupDragController) dragController;
		for (DropController dropController : this.dropControllers) {
			this.dragController.registerDropController(dropController);
		}
	}

	public Widget getBuildedWidget() {
		return StatusGui.instance;
	}

	public void update(Object sender) {
		System.out.println("StatusGUI.Update");

		if (((Map<?, ?>) sender).containsKey(NotificationsConstants.DECORATED_PROJECTS)) {
			toggleSearch.setDown(ProjectStatusFilterHelper.getInstance().isSearch());
		}

	}

	public void display(List<?> updatedBeans) {
		// TODO Auto-generated method stub
		
	}

}
